package com.ptu.controller.user;

import com.ptu.dto.CommentDTO;
import com.ptu.dto.CommentPageDTO;
import com.ptu.dto.CommentUpdateDTO;
import com.ptu.dto.UserDTO;
import com.ptu.entity.Comment;
import com.ptu.server.CommentServer;
import com.ptu.server.OSSService;
import com.ptu.util.PageResult;
import com.ptu.util.Result;
import com.ptu.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController("userCommentController")
@Tag(name = "评论模块")
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentServer commentServer;

    @Autowired
    OSSService ossService;

    @Operation(summary = "根据订单id查询评论")
    @GetMapping("/get/{id}")
    public Result<Comment> getCommentById(@PathVariable Long orderId) {
        System.out.println("根据订单id查询评论");
        Comment comment = commentServer.getCommentById(orderId);
        return Result.success(comment);
    }

    @PostMapping("/add")
    @Operation(summary = "添加评论")
    public Result<Void> addComment(@RequestBody CommentDTO commentDTO) {
        System.out.println("添加评论");
        commentServer.addComment(commentDTO);
        System.out.println("cg");
        return Result.success();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    public Result<Void> deleteComment(Integer id) {
        System.out.println("删除评论");
        commentServer.deleteComment(id);
        return Result.success();
    }

    @PostMapping("/update")
    @Operation(summary = "修改评论")
    public Result<Void> updateComment(@RequestBody CommentDTO commentDTO) {
        System.out.println("修改评论");
        commentServer.updateComment(commentDTO);
        return Result.success();
    }

    @GetMapping("/myList")
    @Operation(summary = "获取我发布的评论列表")
    public List<Comment> myList() {
        System.out.println("获取我发布的评论列表");
        UserDTO user = UserHolder.getUser();
        List<Comment> list = commentServer.getMyCommentList(user.getId());
        return list;
    }

//    @GetMapping("/list")
//    @Operation(summary = "获取评论列表")
//    public Result<PageResult> list(@RequestBody CommentPageDTO commentPageDTO) {
//        System.out.println("获取评论列表");
//        PageResult pageResult = commentServer.getCommentList(commentPageDTO);
//        return Result.success(pageResult);
//    }

    @Operation(summary = "上传评论图片")
    @PostMapping("/upload/comment")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        System.out.println("上传评论图片");
        try {
            // 验证文件类型
            String contentType = file.getContentType();
            if (contentType == null || !contentType.startsWith("image/")) {
                return Result.error("请上传图片文件");
            }

            // 验证文件大小（2MB）
            if (file.getSize() > 2 * 1024 * 1024) {
                return Result.error("图片大小不能超过2MB");
            }

            // 上传到OSS
            String imageUrl = ossService.uploadCommentImage(file, "comment/");

            return Result.success(imageUrl);
        } catch (IOException e) {
            System.out.println("上传失败" + e);
            return Result.error("上传失败");
        }
    }

}
